﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.DAL;
using OneMile.Common;
using OneMile.Entity.Model;
using System.Text;

namespace YGWebMobileApi.APIs
{
     [MobileAPI(Code = "MC0201", Name = "用户获取商品列表(分页)")]
    public class UGetCommodityListByShopId: BaseAPI, IProcessMessage
    {
         private DALUCommodity dalCommodity;
         protected DALUCommodity DalCommodity
        {
            get
            {
                if (dalCommodity == null)
                {
                    dalCommodity = DALFactory.getInstance().CreateUCommodityDAL();
                }
                return dalCommodity;
            }
        }
        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);

                string ShopsId = reqMsg.Body.GetFieldValue("ShopsId");
                int PageNo = reqMsg.Head.PageNo;
                int PageSize = reqMsg.Head.PageSize;
                int SortType = reqMsg.Body.GetFieldValue("SortType").TInt32();
                int SortField = reqMsg.Body.GetFieldValue("SortField").TInt32();
                if (String.IsNullOrEmpty(ShopsId) || SortType == 0 || SortField == 0 || PageNo==0 ||PageSize==0)
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }
               
                List<ECommodity> list = (List<ECommodity>)DalCommodity.GetCommodityListByShopId(ShopsId,PageNo,PageSize,SortType,SortField);

                for (int i = 0; i < list.Count; i++)
                {
                    var fll = rspMsg.Body.AddFieldList("Commdity");
                    fll.AddField("CouponNum", list[i].CouponNum.TString());
                    fll.AddField("CommodityId", list[i].CommodityId.TString());
                    fll.AddField("CommodityName", list[i].CommodityName);
                    fll.AddField("IsHot", list[i].IsHot ? "1" : "0");
                    fll.AddField("IsNew", list[i].IsNew ? "1" : "0");
                    fll.AddField("Price", list[i].Price.TString("F1"));
                }
                rspMsg.Head.PageNo = PageNo;
                rspMsg.Head.PageSize = PageSize;

                rspMsg.Head.RspCode = MessageCode.Success.Code;
                rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("UGetCommodityListByShopId-" + e);
                throw e;
            }
        }

        #endregion
    }
}